/**
 * Created by 2019/3/18/018
 * Author: Admin
 */
import axios from 'axios'
import {
    message
} from 'antd';

// create an axios instance
const service = axios.create({
    // baseURL: process.env.BASE_API, // api的base_url
    timeout: 20000 ,// request timeout
    headers: {
        'Content-Type': 'application/json;charset=utf-8'
    }
})

// request interceptor
service.interceptors.request.use(config => {

    return config
}, error => {
    // Do something with request error
    message.error('加载超时 Load timeout ',2)
    console.log(error) // for debug
    return Promise.reject(error)
})

// respone interceptor
service.interceptors.response.use(
    // response => response,
    /**
     * 下面的注释为通过response自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
     * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
     */
    data => {// 响应成功关闭loading
        // loadinginstace.close()
        if (data.data.code == 1006) {
            // loadinginstace.close()
            message.error({
                message: '登录失效，请重新登录'
            })
        } else {
            return data
        }
    },
    error => {
        console.log('err' + error)// for debug
        message.error('请求超时 Request timed out ', 2)
        return Promise.reject(error)
    })

export default service